package oj.hot100;

public class 相交链表 {
    public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
     }
 }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA,ps = headB;
        int la = 0,lb = 0,len = 0;
        //遍历两个链表,拿到各自链表长度
        while(pl != null) {
            pl = pl.next;
            la++;
        }
        while(ps != null) {
            ps = ps.next;
            lb++;
        }
        len = la-lb;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lb - la;
        }else {
            pl = headA;
            ps = headB;
        }
        //让长的链表先移动len位
        while(len > 0) {
            pl = pl.next;
            len--;
        }
        while(pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }
}
